package pl.edu.pw.elka.tinyrepo.dataserver;

import java.io.File;

import pl.edu.pw.elka.tinyrepo.common.ArgumentLineParser;
import pl.edu.pw.elka.tinyrepo.gate.GateMain;

/**
 * Parser linii argumentow dla data server.
 * 
 * @author Piotr Jarosik
 */
public class DataServerArgumentLineParser extends ArgumentLineParser {
   private Integer port = 9009;
   /** Root directory dla data servera. Domyslnie katalog domowy. */
   private String directory = System.getProperty("user.home")+"/ds_files"; 
   private String gateName = "localhost";
   
   public DataServerArgumentLineParser() {
      addHandler('p', new PortArgumentHandler());
      addHandler('d', new DirectoryArgumentHandler());
      addHandler('g', new GateArgumentHandler());
   }
   
   Integer getPort() {
      return port;
   }

   @Override
   public void setPort(Integer port) {
      this.port = port;
   }
   
   String getDirectory() {
      return directory;
   }
   
   String getGateName() {
      return gateName;
   }
   
   /**
    * Parser katalogu z linii komend
    * @author Piotr Jarosik
    */
   class DirectoryArgumentHandler implements ArgumentHandler {

      @Override
      public void handle(String value) {
         File f = new File(value);
         if(!f.isDirectory()) 
            System.err.println("Argument line parser: wrong root directory: "+value+" using defualt.");
         else directory = value;
      }
   }
   
   /**
    * Parser nazwy hosta na ktorym znajduje sie brama.
    * 
    * @author Piotr Jarosik
    */
   class GateArgumentHandler implements ArgumentHandler {
      @Override
      public void handle(String value) {
         gateName = value;
      }
      
   }
}
